#!/bin/sh 

if [ -n "$DEBUG" ]; then
    set -x
fi

set -e
set +h
set -o nounset 

pwd=$PWD
user=$USER
term=$TERM
home=$HOME
unset `env | awk -F= '/^\w/ {print $1}' | xargs`

TARGET_ARCH="x86_64"

case $TARGET_ARCH in
	i686)
		KARCH=i386
		ABI="gnu"
		;;
	x86_64)
		KARCH=$TARGET_ARCH
		BUILD64="-m64"
		ABI="gnu"
		;;
	arm)
		KARCH=$TARGET_ARCH
		ABI="gnueabi"
		;;
	*)
		KARCH=$TARGET_ARCH
		ABI="gnu"
		;;
esac

export PWD=$pwd
export USER=$user
export TERM=$term
export HOME=$home

CLFS=/mnt/clfs
TOOLS=/tools
CROSS_TOOLS=/cross-tools
export PATH=/usr/lib/ccache/bin:$CROSS_TOOLS/bin:/bin:/usr/bin
CLFS_HOST="$(arch)-cross-linux-gnu"
CLFS_TARGET="${TARGET_ARCH}-linux-${ABI}"

export MAKFLAGS="-j3"

TOOLS_CONFIGURE="\
	--prefix=$TOOLS \
	--build=${CLFS_HOST} \
	--host=${CLFS_TARGET} \
	--cache-file=config.cache"

GNU_MIRROR="http://mirrors.kernel.org/gnu"

cache=$PWD/cache
plans=$PWD/plans
builds=$cache/builds
sources=$cache/sources
stages=$cache/stages
logs=$PWD/logs
log=$PWD/logs/bootstrap.log

init() {
	for d in $logs $builds $sources $stages ; do
		if [ ! -d $d ]; then
			log $FUNCNAME $d
			mkdir -p $d
		fi
	done
}

source_plan() {
	unset name version build_name source 
	source $plans/*/$1/plan
	log "starting" $name-$version
}

log() {
	local action=$1
	local details=$2
    local blue=$(tput setaf 4)
    local reset=$(tput sgr0)
    printf "${blue}bootstrap: ${reset}%-20.20s %s\n" "$action" "$details"
}

download() {
	local url=$1
	local file=$(basename $url)
	_pushd $sources
	if [ ! -f $file ]; then
		log $FUNCNAME $url
		curl -f -L -# -O $url
	fi	
	_popd
}

stage() {
	local file=$(basename $1)
	local name=$2
	local version=$3

	if [ -z "${build_name:-}" ]; then 
		build_name=$name-$version
	fi

	log $FUNCNAME $build_name
	_pushd $stages
		if [ ! -d $build_name ]; then
			log decompressing $name-$version
			tar xf $sources/$file
		fi
	_popd

	if [ "${4:-NIL}" != "NIL" ]; then
		log "copying" "not a gnu_build"
		cp -a $stages/$build_name $builds
	else
		mkdir $builds/$build_name || true
	fi
}

_pushd() {
    builtin pushd "$@" > /dev/null
}

_popd() {
    builtin popd &> /dev/null
}

cross_linux_api() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version" "true"
	_pushd $builds/$build_name
		install -dv $TOOLS/include
		make mrproper
		# FIXME: do not hardcode arch
		make ARCH=$KARCH headers_check
		make ARCH=$KARCH INSTALL_HDR_PATH=dest headers_install
		cp -rv dest/include/* $TOOLS/include
	_popd
	log $FUNCNAME finished
}

cross_file() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name
		$stages/$build_name/configure --prefix=$CROSS_TOOLS
		make
		make install
	_popd
	log $FUNCNAME finished
}

cross_m4() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name
		$stages/$build_name/configure --prefix=$CROSS_TOOLS
		make
		make install
	_popd
	log $FUNCNAME finished
}

cross_ncurses() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $stages/$build_name
		download http://patches.cross-lfs.org/dev/ncurses-5.9-bash_fix-1.patch
		patch -Np1 -i $sources/ncurses-5.9-bash_fix-1.patch
	_popd
	_pushd $builds/$build_name
		$stages/$build_name/configure --prefix=$CROSS_TOOLS \
			--without-debug --without-shared

		make -C include
		make -C progs tic
		install -v -m755 progs/tic $CROSS_TOOLS/bin
	_popd
	log $FUNCNAME finished
}

cross_gmp() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name
		CPPFLAGS=-fexceptions $stages/$build_name/configure \
			--prefix=$CROSS_TOOLS \
			--enable-cxx
		make
		make install
	_popd
	log $FUNCNAME finished
}

cross_mpfr() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name
	LDFLAGS="-Wl,-rpath,$CROSS_TOOLS/lib" \
		$stages/$build_name/configure \
		--prefix=$CROSS_TOOLS \
		--enable-shared \
		--with-gmp=$CROSS_TOOLS
		make
		make install
	_popd
	log $FUNCNAME finished
}

cross_mpc() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name
	LDFLAGS="-Wl,-rpath,$CROSS_TOOLS/lib" \
		$stages/$build_name/configure --prefix=$CROSS_TOOLS \
		--with-gmp=$CROSS_TOOLS \
		--with-mpfr=$CROSS_TOOLS
	make
	make install
	_popd
	log $FUNCNAME finished
}

cross_ppl() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name
	CPPFLAGS="-I$CROSS_TOOLS/include" \
		LDFLAGS="-Wl,-rpath,$CROSS_TOOLS/lib" \
		$stages/$build_name/configure --prefix=$CROSS_TOOLS \
		--enable-shared \
		--enable-interfaces="c,cxx" \
		--disable-optimization \
		--with-libgmp-prefix=$CROSS_TOOLS \
		--with-libgmpxx-prefix=$CROSS_TOOLS
		make
		make install
	_popd
	log $FUNCNAME finished
}

cross_cloog_ppl() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"

	_pushd $stages/$build_name
		cp -v configure{,.orig}
		sed -e "/LD_LIBRARY_PATH=/d" \
			configure.orig > configure
	_popd

	_pushd $builds/$build_name
		LDFLAGS="-Wl,-rpath,$CROSS_TOOLS/lib" \
			$stages/$build_name/configure \
			--prefix=$CROSS_TOOLS \
			--enable-shared \
			--with-bits=gmp \
			--with-gmp=$CROSS_TOOLS \
			--with-ppl=$CROSS_TOOLS
		make 
		make install
	_popd

	log $FUNCNAME finished
}

cross_binutils() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name

	case $TARGET_ARCH in
		x86_64) extra_flags="--enable-64-bit-bfd" ;;
	esac

	AR=ar AS=as \
		$stages/$build_name/configure \
		--prefix=$CROSS_TOOLS \
		--host=${CLFS_HOST} \
		--target=${CLFS_TARGET} \
		--with-sysroot=$CLFS \
		--with-lib-path=$TOOLS/lib \
		--disable-nls  \
		--enable-shared \
		--disable-multilib ${extra_flags:-}

	make configure-host
	make
	make install
	cp -v $stages/$build_name/include/libiberty.h /tools/include
	_popd
	log $FUNCNAME finished
}

cross_gcc() {
	source_plan $1
	download $source
	download http://patches.cross-lfs.org/dev/gcc-4.6.0-specs-1.patch
	download http://patches.cross-lfs.org/dev/gcc-4.6.0-pure64_specs-1.patch
	stage "$source" "$name" "$version"
	_pushd $stages/$build_name
	
		case $TARGET_ARCH in
			x86_64) patch -Np1 -i $sources/gcc-4.6.0-pure64_specs-1.patch ;;
			*) patch -Np1 -i $sources/gcc-4.6.0-specs-1.patch ;;
		esac

		echo -en '#undef STANDARD_INCLUDE_DIR\n#define STANDARD_INCLUDE_DIR "/tools/include/"\n\n' >> gcc/config/linux.h
		echo -en '\n#undef STANDARD_STARTFILE_PREFIX_1\n#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"\n' >> gcc/config/linux.h
		echo -en '\n#undef STANDARD_STARTFILE_PREFIX_2\n#define STANDARD_STARTFILE_PREFIX_2 ""\n' >> gcc/config/linux.h

		cp -v gcc/Makefile.in{,.orig}
		sed -e "s@\(^CROSS_SYSTEM_HEADER_DIR =\).*@\1 /tools/include@g" \
			gcc/Makefile.in.orig > gcc/Makefile.in

		touch /tools/include/limits.h
	_popd

	_pushd $builds/$build_name
	AR=ar LDFLAGS="-Wl,-rpath,$CROSS_TOOLS/lib" \
		$stages/$build_name/configure --prefix=$CROSS_TOOLS \
		--build=${CLFS_HOST} --host=${CLFS_HOST} --target=${CLFS_TARGET} \
		--with-sysroot=$CLFS --with-local-prefix=$TOOLS --disable-nls \
		--disable-shared --with-mpfr=$CROSS_TOOLS --with-gmp=$CROSS_TOOLS \
		--without-headers --with-newlib --disable-decimal-float \
		--disable-libgomp --disable-libmudflap --disable-libssp \
		--disable-threads --enable-languages=c --disable-multilib --disable-bootstrap
		#--with-ppl=$CROSS_TOOLS \
		#--with-cloog=$CROSS_TOOLS \
		make all-gcc all-target-libgcc
		make install-gcc install-target-libgcc
	_popd

	log $FUNCNAME finished
}

eglibc() {
	log $FUNCNAME started
	name=eglibc
	version=2.14
	build_name=$name-$version
	#source=http://cross-lfs.org/files/packages/git/eglibc-$version-r13356.tar.bz2
	#download $source
	#stage "$source" "$name" "$version"
	[ ! -d $stages/$build_name ] && cp -af $sources/$build_name $stages/
	[ ! -d $builds/$build_name ] && mkdir -p $builds/$build_name
	ln -sf ../ports $stages/$build_name/libc/ports

	_pushd $stages/$build_name/libc
		cp -v Makeconfig{,.orig}
			sed -e 's/-lgcc_eh//g' Makeconfig.orig > Makeconfig
	_popd

	_pushd $builds/$build_name

cat > config.cache << "EOF"
libc_cv_forced_unwind=yes
libc_cv_c_cleanup=yes
libc_cv_gnu89_inline=yes
libc_cv_ssp=no
EOF

		BUILD_CC="gcc" CC="${CLFS_TARGET}-gcc ${BUILD64:-}" \
			AR="${CLFS_TARGET}-ar" RANLIB="${CLFS_TARGET}-ranlib" \
			$stages/$build_name/libc/configure --prefix=$TOOLS \
			--host=${CLFS_TARGET} --build=${CLFS_HOST} \
			--disable-profile --enable-add-ons \
			--with-tls --enable-kernel=2.6.0 --with-__thread \
			--with-binutils=$CROSS_TOOLS/bin --with-headers=$TOOLS/include \
			--cache-file=config.cache

	make
	make install

	_popd

	log $FUNCNAME finished
}

cross_gcc_final() {
	source_plan $1
	download $source
	download http://patches.cross-lfs.org/dev/gcc-4.6.0-specs-1.patch
	download http://patches.cross-lfs.org/dev/gcc-4.6.0-pure64_specs-1.patch
	stage "$source" "$name" "$version"
	
	_pushd  $stages/$build_name

		case $TARGET_ARCH in
			x86_64) patch -Np1 -i $sources/gcc-4.6.0-pure64_specs-1.patch ;;
			*) patch -Np1 -i $sources/gcc-4.6.0-specs-1.patch ;;
		esac


		echo -en '#undef STANDARD_INCLUDE_DIR\n#define STANDARD_INCLUDE_DIR "/tools/include/"\n\n' >> gcc/config/linux.h
		echo -en '\n#undef STANDARD_STARTFILE_PREFIX_1\n#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"\n' >> gcc/config/linux.h
		echo -en '\n#undef STANDARD_STARTFILE_PREFIX_2\n#define STANDARD_STARTFILE_PREFIX_2 ""\n' >> gcc/config/linux.h

		cp -v gcc/Makefile.in{,.orig}
		sed -e "s@\(^CROSS_SYSTEM_HEADER_DIR =\).*@\1 /tools/include@g" \
			gcc/Makefile.in.orig > gcc/Makefile.in
	_popd

	_pushd $builds/$build_name
	AR=ar LDFLAGS="-Wl,-rpath,$CROSS_TOOLS/lib" \
		$stages/$build_name/configure --prefix=$CROSS_TOOLS \
		--build=${CLFS_HOST} --target=${CLFS_TARGET} --host=${CLFS_HOST} \
		--with-sysroot=${CLFS} --with-local-prefix=$TOOLS --disable-nls \
		--enable-shared --enable-languages=c,c++ --enable-__cxa_atexit \
		--with-mpfr=$CROSS_TOOLS --with-gmp=/cross-tools --enable-c99 \
		--enable-long-long --enable-threads=posix --disable-multilib --disable-bootstrap
		#--with-ppl=$CROSS_TOOLS --with-cloog=$CROSS_TOOLS \

		make AS_FOR_TARGET="${CLFS_TARGET}-as" \
			LD_FOR_TARGET="${CLFS_TARGET}-ld"
		make install
	_popd

	log $FUNCNAME finished
}



# TOOLS
tools_gmp() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name
	HOST_CC=gcc CPPFLAGS=-fexceptions \
		$stages/$build_name/configure $TOOLS_CONFIGURE \
			--enable-cxx
		make
		make install
	_popd
	log $FUNCNAME finished
}

tools_mpfr() {
	source_plan $1
	source $plans/*/$1/plan
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name
		$stages/$build_name/configure $TOOLS_CONFIGURE \
				--enable-shared --with-gmp=$TOOLS
		make
		make install
	_popd
	log $FUNCNAME finished
}


tools_zlib() {
	source_plan $1
	source $plans/*/$1/plan
	download $source
	stage "$source" "$name" "$version" "true"
	_pushd $builds/$build_name
		$stages/$build_name/configure --prefix=$TOOLS
		make
		make install
	_popd
	log $FUNCNAME finished
}

tools_binutils() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name

		case $TARGET_ARCH in
			x86_64) extra_flags="--enable-64-bit-bfd" ;;
		esac

		CC="${CC} ${BUILD64:-}" \
			$stages/$build_name/configure $TOOLS_CONFIGURE \
			--target=$CLFS_TARGET --with-lib-path=$TOOLS/lib \
			--disable-nls --enable-shared \
			--disable-multilib ${extra_flags:-}
		make configure-host
		make
		make install
	_popd
	log $FUNCNAME finished
}

tools_libgcc() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"

	_pushd $builds/$build_name
		CC="${CC} ${BUILD64:-}" CXX="${CXX} ${BUILD64:-}"  \
			$stages/$build_name/configure $TOOLS_CONFIGURE --target=${CLFS_TARGET} \
			--with-local-prefix=$TOOLS --enable-long-long --enable-c99 \
			--enable-shared --enable-threads=posix --enable-__cxa_atexit \
			--disable-nls --enable-languages=c --disable-libstdcxx-pch \
			--disable-multilib --disable-bootstrap

		make AS_FOR_TARGET="${AS}" LD_FOR_TARGET="${LD}" all-target-libgcc
		make install-target-libgcc
}

tools_gcc() {
	source_plan $1
	download $source

	download http://patches.cross-lfs.org/dev/gcc-4.6.0-branch_update-1.patch
	download http://patches.cross-lfs.org/dev/gcc-4.6.0-specs-1.patch

	stage "$source" "$name" "$version"

	_pushd  $stages/$build_name

	#patch -Np1 -i $sources/gcc-4.6.0-branch_update-1.patch
	patch -Np1 -i $sources/gcc-4.6.0-specs-1.patch

	echo -en '#undef STANDARD_INCLUDE_DIR\n#define STANDARD_INCLUDE_DIR "/tools/include/"\n\n' >> gcc/config/linux.h
	echo -en '\n#undef STANDARD_STARTFILE_PREFIX_1\n#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"\n' >> gcc/config/linux.h
	echo -en '\n#undef STANDARD_STARTFILE_PREFIX_2\n#define STANDARD_STARTFILE_PREFIX_2 ""\n' >> gcc/config/linux.h

	cp -v gcc/Makefile.in{,.orig}
	sed -e 's@\(^NATIVE_SYSTEM_HEADER_DIR =\).*@\1 /tools/include@g' \
		gcc/Makefile.in.orig > gcc/Makefile.in

	_popd

	_pushd $builds/$build_name
		CC="${CC} ${BUILD64:-}" CXX="${CXX} ${BUILD64:-}"  \
			$stages/$build_name/configure $TOOLS_CONFIGURE --target=${CLFS_TARGET} \
			--with-local-prefix=$TOOLS --enable-long-long --enable-c99 \
			--enable-shared --enable-threads=posix --enable-__cxa_atexit \
			--disable-nls --enable-languages=c,c++ --disable-libstdcxx-pch \
			--disable-multilib --disable-bootstrap

		cp -v Makefile{,.orig}
		sed "/^HOST_\(GMP\|PPL\|CLOOG\)\(LIBS\|INC\)/s:-[IL]/\(lib\|include\)::" \
			Makefile.orig > Makefile

		make AS_FOR_TARGET="${AS}" \
			LD_FOR_TARGET="${LD}"
		make install

	_popd

	log $FUNCNAME finished
}

tools_ncurses() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name
		$stages/$build_name/configure $TOOLS_CONFIGURE \
			--without-debug --without-ada \
			--enable-overwrite --with-build-cc=gcc \
			--with-shared
		make
		make install
	_popd
	log $FUNCNAME finished
}



tools_bash() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name

cat > config.cache << "EOF"
ac_cv_func_mmap_fixed_mapped=yes
ac_cv_func_strcoll_works=yes
ac_cv_func_working_mktime=yes
bash_cv_func_sigsetjmp=present
bash_cv_getcwd_malloc=yes
bash_cv_job_control_missing=present
bash_cv_printf_a_format=yes
bash_cv_sys_named_pipes=present
bash_cv_ulimit_maxfds=yes
bash_cv_under_sys_siglist=yes
bash_cv_unusable_rtsigs=no
gt_cv_int_divbyzero_sigfpe=yes
EOF

		$stages/$build_name/configure $TOOLS_CONFIGURE \
			--without-bash-malloc --cache-file=config.cache

		make
		make install
		ln -sfv bash $TOOLS/bin/sh
	_popd
	log $FUNCNAME finished
}

tools_bzip2() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version" "true"
	_pushd $builds/$build_name
		cp -v Makefile{,.orig}
		sed -e 's@^\(all:.*\) test@\1@g' Makefile.orig > Makefile

		make CC="${CC}" AR="${AR}" RANLIB="${RANLIB}"
		make PREFIX=$TOOLS install
	_popd
	log $FUNCNAME finished
}

tools_coreutils() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name
		mkdir man || true
		touch man/uname.1 man/hostname.1

cat > config.cache << EOF
fu_cv_sys_stat_statfs2_bsize=yes
gl_cv_func_working_mkstemp=yes
EOF
		$stages/$build_name/configure $TOOLS_CONFIGURE \
			--enable-install-program=hostname --cache-file=config.cache

		make 
		make install
	_popd
	log $FUNCNAME finished
}

tools_findutils() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name
		echo "gl_cv_func_wcwidth_works=yes" > config.cache
		echo "ac_cv_func_fnmatch_gnu=yes" >> config.cache

		$stages/$build_name/configure $TOOLS_CONFIGURE --cache-file=config.cache

		make
		make install
	_popd
	log $FUNCNAME finished
}

tools_flex() {
	source_plan $1
	download $source
	download http://patches.cross-lfs.org/dev/flex-2.5.35-gcc44-1.patch
	stage "$source" "$name" "$version"

	_pushd $stages/$build_name
		patch -Np1 -i $sources/flex-2.5.35-gcc44-1.patch
	_popd

	_pushd $builds/$build_name

cat > config.cache << EOF
ac_cv_func_malloc_0_nonnull=yes
ac_cv_func_realloc_0_nonnull=yes
EOF
		$stages/$build_name/configure $TOOLS_CONFIGURE

		make
		make install
	_popd
	log $FUNCNAME finished
}

tools_gettext() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version" "true"
	_pushd $builds/$build_name
		_pushd gettext-tools
			echo "gl_cv_func_wcwidth_works=yes" > config.cache

			./configure $TOOLS_CONFIGURE \
				--disable-shared
			make -C gnulib-lib
			make -C src msgfmt
			cp -v src/msgfmt $TOOLS/bin
		_popd
	_popd
	log $FUNCNAME finished
}

tools_grep() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version" "true"
	_pushd $builds/$build_name

cat > config.cache << EOF
ac_cv_func_malloc_0_nonnull=yes
ac_cv_func_realloc_0_nonnull=yes
EOF
		$stages/$build_name/configure $TOOLS_CONFIGURE \
			--disable-perl-regexp --without-included-regexp 

		make
		make install
	_popd
	log $FUNCNAME finished
}

tools_m4() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name

cat > config.cache << EOF
gl_cv_func_btowc_eof=yes
gl_cv_func_mbrtowc_incomplete_state=yes
gl_cv_func_mbrtowc_sanitycheck=yes
gl_cv_func_mbrtowc_null_arg=yes
gl_cv_func_mbrtowc_retval=yes
gl_cv_func_mbrtowc_nul_retval=yes
gl_cv_func_wcrtomb_retval=yes
gl_cv_func_wctob_works=yes
EOF
		$stages/$build_name/configure $TOOLS_CONFIGURE

		make
		make install
	_popd
	log $FUNCNAME finished
}

tools_generic() {
	source_plan $1
	log $name-$version started
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name

		$stages/$build_name/configure $TOOLS_CONFIGURE ${2:-}

		make
		make install
	_popd
	log $name-$version finished
}

tools_patch() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name

		echo "ac_cv_func_strnlen_working=yes" > config.cache

		$stages/$build_name/configure $TOOLS_CONFIGURE

		make
		make install
	_popd
	log $FUNCNAME finished
}

tools_texinfo() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version"
	_pushd $builds/$build_name

		$stages/$build_name/configure $TOOLS_CONFIGURE

		make -C tools/gnulib/lib
		make -C tools
		make
		make install
	_popd
	log $FUNCNAME finished
}

tools_vim() {
	source_plan $1
	download $source
	stage "$source" "$name" "$version" "true"

	_pushd $builds/$build_name

	sed -i "/using uint32_t/s/as_fn_error/#&/" src/auto/configure

cat > src/auto/config.cache << "EOF"
vim_cv_getcwd_broken=no
vim_cv_memmove_handles_overlap=yes
vim_cv_stat_ignores_slash=no
vim_cv_terminfo=yes
vim_cv_tgent=zero
vim_cv_toupper_broken=no
vim_cv_tty_group=world
ac_cv_sizeof_int=4
ac_cv_sizeof_long=4
ac_cv_sizeof_time_t=4
ac_cv_sizeof_off_t=4
EOF

		./configure $TOOLS_CONFIGURE \
			--enable-multibyte --enable-gui=no \
			--disable-gtktest --disable-xim --with-features=normal \
			--disable-gpm --without-x --disable-netbeans \
			--with-tlib=ncurses

		make
		make install
		ln -sfv vim $TOOLS/bin/vi

cat > $TOOLS/etc/vimrc << "EOF"
" Begin /etc/vimrc

set nocompatible
set backspace=2
set ruler
syntax on

" End /etc/vimrc
EOF

	_popd
	log $FUNCNAME finished
}

tools_perl() {
	source_plan $1
	download $source
	download http://patches.cross-lfs.org/dev/perl-5.14.0-libc-1.patch
	stage "$source" "$name" "$version"
	
	_pushd $stages/$build_name
		patch -Np1 -i $sources/perl-5.14.0-libc-1.patch
		sed -i 's@/usr/include@/tools/include@g' ext/Errno/Errno_pm.PL

	_popd

	_pushd $builds/$build_name

		$stages/$build_name/configure.gnu $TOOLS_CONFIGURE -Dcc="gcc"

		make
		make install
	_popd
	log $FUNCNAME finished
}

cross_phase() {
	init
	cross_linux_api linux
	cross_gmp gmp
	cross_mpfr mpfr
	cross_mpc mpc
	cross_binutils binutils
	cross_gcc gcc
	./via cross_build eglibc
	make clean
	init
	cross_gcc_final gcc
	make clean
}

tools_phase() {
	init
	export CC="${CLFS_TARGET}-gcc"
	export CXX="${CLFS_TARGET}-g++"
	export AR="${CLFS_TARGET}-ar"
	export AS="${CLFS_TARGET}-as"
	export RANLIB="${CLFS_TARGET}-ranlib"
	export LD="${CLFS_TARGET}-ld"
	export STRIP="${CLFS_TARGET}-strip"
	
	while read line; do
		if [[ ${line::1} == "#" ]] || [[ ${line::1} == "" ]]; then
			continue
		fi
		name_version="$(./via get_version $line)"
		if [[ -d /tools/var/db/via/$name_version ]]; then
			continue
		fi
		./via cross_build $line
	done < "cross/tools.list"
	tools_gcc gcc
	ln -svf . /tools/tools
}
init
cross_phase
tools_phase
